package com.owen.l5;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 给定一个字符串 s，找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
 *
 * 示例 1：
 *
 * 输入: "babad"
 * 输出: "bab"
 * 注意: "aba" 也是一个有效答案。
 * 示例 2：
 *
 * 输入: "cbbd"
 * 输出: "bb"
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/longest-palindromic-substring
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class MySolution1 {
    public String longestPalindrome(String s) {
        int strLength=s.length();
        for (int i = strLength; i > 0 ; i--) {
            List<String> begin = getBegin(s,i);
            for (int i1 = 0; i1 < begin.size(); i1++) {
                String s1 = begin.get(i1);
                if(isPalindrome(s1)){
                    return s1;
                }
            }
        }
        return "";
    }

    private List<String> getBegin(String s, int length) {
        List<String> result=new ArrayList<>();
        int strLength=s.length();
        for(int i=0;i<strLength-length+1;i++){
            result.add(getBeginSubstr(s,i,length));
        }
        return result;
    }

    private String getBeginSubstr(String s,int begin,int length){
        char[] result=new char[length];
        for (int i = begin,j=0; i < begin+length; i++) {
            result[j++]=s.charAt(i);
        }
        return new String(result);
    }

    private boolean isPalindrome(String s){
        int begin=0,last=s.length()-1;
        while (begin<=last){
            if(s.charAt(begin)==s.charAt(last)){
                begin++;
                last--;
            }else{
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String aacabdkacaa = new MySolution1().longestPalindrome("kyyrjtdplseovzwjkykrjwhxquwxsfsorjiumvxjhjmgeueafubtonhlerrgsgohfosqssmizcuqryqomsipovhhodpfyudtusjhonlqabhxfahfcjqxyckycstcqwxvicwkjeuboerkmjshfgiglceycmycadpnvoeaurqatesivajoqdilynbcihnidbizwkuaoegmytopzdmvvoewvhebqzskseeubnretjgnmyjwwgcooytfojeuzcuyhsznbcaiqpwcyusyyywqmmvqzvvceylnuwcbxybhqpvjumzomnabrjgcfaabqmiotlfojnyuolostmtacbwmwlqdfkbfikusuqtupdwdrjwqmuudbcvtpieiwteqbeyfyqejglmxofdjksqmzeugwvuniaxdrunyunnqpbnfbgqemvamaxuhjbyzqmhalrprhnindrkbopwbwsjeqrmyqipnqvjqzpjalqyfvaavyhytetllzupxjwozdfpmjhjlrnitnjgapzrakcqahaqetwllaaiadalmxgvpawqpgecojxfvcgxsbrldktufdrogkogbltcezflyctklpqrjymqzyzmtlssnavzcquytcskcnjzzrytsvawkavzboncxlhqfiofuohehaygxidxsofhmhzygklliovnwqbwwiiyarxtoihvjkdrzqsnmhdtdlpckuayhtfyirnhkrhbrwkdymjrjklonyggqnxhfvtkqxoicakzsxmgczpwhpkzcntkcwhkdkxvfnjbvjjoumczjyvdgkfukfuldolqnauvoyhoheoqvpwoisniv");
        System.out.println(aacabdkacaa);
    }
}
